package com.hitalkie.teacher.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hitalkie.teacher.R;
import com.hitalkie.teacher.app.TalkieApi;
import com.hitalkie.teacher.fragment.LoadingDialogFragment;
import com.hitalkie.teacher.model.BaseResponse;
import com.hitalkie.teacher.util.OkHttpManager;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Date 06/01/2017
 *
 * @author charliema
 */

public class WithdrawalActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        setContentView(R.layout.view_withdrawal);
        initTitleBar();
        findViewById(R.id.tvConfirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                withdraw();
            }
        });
    }

    private void initTitleBar() {
        findViewById(R.id.viewTitleBarLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
    }

    private void initData() {

    }

    private void withdraw() {
        EditText etName = (EditText) findViewById(R.id.etName);
        EditText etAddress = (EditText) findViewById(R.id.etAddress);
        EditText etAmount = (EditText) findViewById(R.id.etAmount);

        String strName = etName.getText().toString();
        String strAddress = etAddress.getText().toString();
        String strAmount = etAmount.getText().toString();

        if (TextUtils.isEmpty(strName)) {
            etName.setError(getText(R.string.can_not_be_null));
            return;
        }
        if (TextUtils.isEmpty(strAddress)) {
            etAddress.setError(getText(R.string.can_not_be_null));
            return;
        }
        if (TextUtils.isEmpty(strAmount)) {
            etAmount.setError(getText(R.string.can_not_be_null));
            return;
        }
        withdraw(strName, strAddress, strAmount);

    }

    private void withdraw(String name, String address, String amount) {
        // loading dialog
        final LoadingDialogFragment loadingDialogFragment = new LoadingDialogFragment();
        loadingDialogFragment.show(getSupportFragmentManager(), "loading");

        Map<String, String> map = new HashMap<>();
        map.put("account_name", name);
        map.put("current_address", address);
        map.put("amount", amount);
        String url = TalkieApi.getHost() + TalkieApi.PATH_WITHDRAW;
        Request.Builder reqBuilder = new Request.Builder().url(url).post(RequestBody.create(OkHttpManager.JSON, new Gson().toJson(map)));
        OkHttpManager.client.newCall(reqBuilder.build())
                //
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                // loading dialog
                                loadingDialogFragment.dismiss();
                                Toast.makeText(getActivity(), getText(R.string.network_exception), Toast.LENGTH_LONG).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        // loading dialog
                        loadingDialogFragment.dismiss();
                        if (!response.isSuccessful()) {
                            return;
                        }
                        String jsonStr = response.body().string();

                        try {
                            final BaseResponse resp = new Gson().fromJson(jsonStr, BaseResponse.class);
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (resp.status.error_code == 0) {
                                        Toast.makeText(getActivity(), getText(R.string.submit_succeeded), Toast.LENGTH_LONG).show();
                                        getActivity().finish();
                                    } else {
                                        Toast.makeText(getActivity(), resp.status.error_message, Toast.LENGTH_LONG).show();
                                    }

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            // loading dialog
                            loadingDialogFragment.dismiss();
                        }
                    }
                });
    }

}
